package com.ysq.hebt.webview.service.imple;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ysq.hebt.common.util.CommonUtil;
import com.ysq.hebt.common.util.DateUtil;
import org.springframework.stereotype.Service;
import com.ysq.hebt.basic.impl.BasicServiceSupportImpl;
import com.ysq.hebt.common.entity.LoginUser;
import com.ysq.hebt.webview.entity.PwdMemoInfo;
import com.ysq.hebt.webview.service.IPwdMemoService;

/**
 * 密码备忘
 * @author Administrator
 *
 */
@Service("pwdMemoService")
public class PwdMemoServiceImpl extends BasicServiceSupportImpl implements IPwdMemoService {
	
	String mapperName = "webview.pwdMemoMapper";
	
	/**
	 * 查询当前用户的密码备忘列表
	 * @param user
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String getPwdMemoList(LoginUser user, int start, int limit){
		
		String result = null;

		int userId = user.getUserId();

		Map<String, Object> parameterMap = new HashMap<String, Object>();
		
		parameterMap.put("start", start);
		parameterMap.put("limit", limit);
		parameterMap.put("userId", userId);

		try {
			int total = (Integer)basicDao.findForObject(mapperName + ".getPwdMemoTotal", parameterMap);
			
			List<PwdMemoInfo> userList = (List<PwdMemoInfo>)basicDao.findForList(mapperName + ".findUserPwdMemo", parameterMap);
			
			result = CommonUtil.getGridPageJson(total, userList);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 插入新的备忘
	 * @param user
	 * @param pwdMemoInfo
	 * @return
	 */
	public String insertFieldView(LoginUser user, PwdMemoInfo pwdMemoInfo){
		String formatType = "yyyy-MM-dd HH:mm:ss";
		
		String resultJson = null;
		boolean flag = false;
		String msg = "保存失败";
		
		String createTime = DateUtil.getNowDay(formatType);
		
		pwdMemoInfo.setUserId(user.getUserId());
		pwdMemoInfo.setCreateTime(createTime);
		
		try {
			basicDao.save(mapperName + ".insertPwdMemo", pwdMemoInfo);
			
			flag = true;
			msg = "保存成功";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		resultJson = CommonUtil.returnAjaxJson(flag, msg);
		return resultJson;
	}
	
	
	/**
	 * 修改memo
	 * @param user
	 * @param pwdMemoInfo
	 * @return
	 */
	public String editPwdMemo(LoginUser user, PwdMemoInfo pwdMemoInfo){
		String formatType = "yyyy-MM-dd HH:mm:ss";
		String resultJson = null;
		boolean flag = false;
		String msg = "保存失败";
		
		String createTime = DateUtil.getNowDay(formatType);
		
		pwdMemoInfo.setUserId(user.getUserId());
		pwdMemoInfo.setCreateTime(createTime);
		
		try {
			basicDao.update(mapperName + ".editPwdMemo", pwdMemoInfo);
			flag = true;
			msg = "保存成功";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		resultJson = CommonUtil.returnAjaxJson(flag, msg);
		return resultJson;
	}
	
	/**
	 * 删除 memos
	 * @param idArrayStr
	 * @return
	 */
	public String deletePwdMemos(String idArrayStr){
		String resultJson = null;
		boolean flag = false;
		String msg = "删除失败";
		
		try {
			//删用户
			basicDao.delete(mapperName + ".deletePwdMemos", idArrayStr);
			flag = true;
			msg = "删除成功";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		resultJson = CommonUtil.returnAjaxJson(flag, msg);
		
		return resultJson;
	}
}
